import { Component,Injector  } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL} from "./../../providers/Config";

/**
 * Generated class for the ApplicationPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-application',
  templateUrl: 'application.html',
})
export class ApplicationPage extends BasePage{

  picrooturl="";//图片服务地址
  userinfo={}//获取内存中的数据
  orderInfo={};  //从待服务里接收到的服务订单详情
  serviceInfo={}  //从订单详情里取出的服务详情
  content= '';  //退款说明
  isOk:boolean;   //是否显示√
  reasonList=[{"reason":"无理由退款","select":true,"selectNo":"assets/img/Comment/xuankuang@2x.png","selectOk":"assets/img/Comment/right@2x.png"},
              {"reason":"后悔了/不想要了","select":false,"selectNo":"assets/img/Comment/xuankuang@2x.png","selectOk":""},
              {"reason":"买多了/订单信息错误","select":false,"selectNo":"assets/img/Comment/xuankuang@2x.png","selectOk":""},
              {"reason":"计划有变/没时间消费","select":false,"selectNo":"assets/img/Comment/xuankuang@2x.png","selectOk":""},
              {"reason":"去过了/款式与颜色描述不符/做工粗糙有瑕疵","select":false,"selectNo":"assets/img/Comment/xuankuang@2x.png","selectOk":""},
              ]

  constructor(injector: Injector,public userService: UserService,public stc: StorageService) {
    super(injector);
    this.picrooturl=PIC_URL
    this.userinfo=this.stc.read('userinfo'); 
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ApplicationPage');
    this.orderInfo=this.navParams.get("item");
    this.serviceInfo=this.orderInfo['service_info'];
     
    console.log(this.orderInfo);
  }
  dismissCLI(i){
    this.stc.write("back",this.navParams.get("par"));
    if(i){
      this.viewCtrl.dismiss(i);
    }else{
      this.viewCtrl.dismiss();
    }
    
  }
  change(){
    console.log("内容改变")
  }
  choose(i){
    //点击时先把所有项都清空
    this.reasonList.forEach((e)=>{
      e.select=false;
      e.selectNo="assets/img/Comment/xuankuang@2x.png";
      e.selectOk="";
    })
    if(this.reasonList[i].select){
      this.reasonList[i].select=false;
      this.reasonList[i].selectNo="assets/img/Comment/xuankuang@2x.png";
      this.reasonList[i].selectOk="";
    }else{
      this.reasonList[i].select=true;
      this.reasonList[i].selectNo="assets/img/Comment/xuankuang@2x.png";
      this.reasonList[i].selectOk="assets/img/Comment/right@2x.png";
    }
  }
  //开始退款
  async apply(){
    let reason1; //官方原因
    let reasonAll; //总原因
    this.reasonList.forEach((e)=>{
      if(e.select){
        reason1=e.reason;
      }
    })
    if(this.content==""){
      reasonAll=reason1;
    }else{
      reasonAll=reason1+"-"+this.content;
    }
    let param={order_sn:this.orderInfo['order_sn'],cancel_reason:reasonAll}
    console.log(param);
  //  let modal = this.modalCtrl.create("ConsumerorderPage", {stu:this.navParams.get("par")});
  //  modal.onDidDismiss(data => {
  //  });
  //  modal.present();
    let result=await this.userService.userRefundAPI(param);
    //成功后传一个值表示成功，失败和不操作均给空值
    if(result.code==1){
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom'
      });
      toast.present();
      setTimeout(()=>{
        this.dismissCLI("ok")
      },1000)
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom'
      });
      toast.present();
    }
  }



}
 